package org.zw.android.framework.impl;

import org.zw.android.framework.cache.IDownloaderCallback;
import org.zw.android.framework.cache.ImageDownloader;
import org.zw.android.framework.cache.ImageOption;
import org.zw.android.framework.cache.RecyclingImageView;
import org.zw.android.framework.cache.ImageDownloader.State;
import org.zw.android.framework.util.StringUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;

/**
 * Asynchronous loading Bitmap Implements
 * 
 * @author zhouwei
 *
 */
public final class ImageCooler {
	
	private static ImageDownloader downloader ;
	
	protected static void config(Context context,
			String cacheName,
			float percent ,
			int maxWidth, 
			int maxHeight){
		
		if(downloader == null){
			downloader = ImageDownloader.create(context, cacheName, percent < 0.01f ? 0.01f : percent,maxWidth,maxHeight);
		}
	}
	
	public static void request(RecyclingImageView imageView, String url, ImageOption op){
		
		if(imageView == null || StringUtils.isEmpty(url)) return ;
		
		if(downloader != null){
			downloader.downloadBitmap(url, imageView, op);
		}
	}

	/**
	 * download bitmap
	 * @param view
	 * @return
	 */
	public static ImageRequest request(RecyclingImageView view){
		
		if(downloader == null){
			Log.e("ImageCool", "ERROR: Please first call ImageCool.config(...)");
			return null ;
		}
		
		if(view == null){
			Log.e("ImageCool", "ERROR: ImageView is null");
			return null ;
		}
		
		return new ImageRequest(view) ;
	}
	
	public static Bitmap getBitmap(String url) {
		return downloader != null ? downloader.getBitmapFromCache(url) : null;
	}

	public static Bitmap getBitmap(String url, ImageOption op) {
		return downloader != null ? downloader.getBitmapFromCache(url,op) : null;
	}

	public static String getCacheLocalPath(String url) {
		return downloader != null ? downloader.getCacheLocalPath(url) : null;
	}

	public static void onStart() {
		if(downloader != null){
			downloader.onStart() ;
		}
	}

	public static void onPause() {
		if(downloader != null){
			downloader.onPause() ;
		}
	}

	public static void onResume() {
		if(downloader != null){
			downloader.onResume() ;
		}
	}

	public static void onDestory() {
		if(downloader != null){
			downloader.onDestory() ;
		}
	}

	public static void setState(State newState) {
		if(downloader != null){
			downloader.setState(newState) ;
		}
	}

	public static State getState() {
		return downloader != null ? downloader.getState() : State.STATE_OFF ;
	}

	public static void clearDiskCache() {
		if(downloader != null){
			downloader.clearDiskCacheFile() ;
		}
	}

	public static void onReset() {
		if(downloader != null){
			downloader.onReset() ;
		}
	}

	public static void onCancelTask() {
		if(downloader != null){
			downloader.onCancelTask() ;
		}
	}
	
	/**
	 * Image Request
	 * @author zhouwei
	 *
	 */
	public static class ImageRequest{
		
		private ImageView				mView ;
		private String 					mUrl ;
		private ImageOption 			mOption ;
		
		public ImageRequest(ImageView view){
			mView		= view ;
			mOption 	= new ImageOption();
		}
		
		public ImageRequest withCallback(IDownloaderCallback callback){
			mOption.setCallback(callback);
			return this ;
		}
		
		public ImageRequest withDefault(BitmapDrawable drawable){
			mOption.setDefault(drawable);
			return this ;
		}
		
		public ImageRequest withDefault(int drawable){
			mOption.setDefaultResId(drawable);
			return this ;
		}
		
		public ImageRequest withError(Drawable drawable){
			mOption.setError(drawable);
			return this ;
		}
		
		public ImageRequest withError(int drawable){
			mOption.setErrorResId(drawable);
			return this ;
		}
		
		public ImageRequest withFadeIn(boolean fadein){
			mOption.setFadeInBitmap(fadein);
			return this ;
		}
		
		public ImageRequest withUrl(String url){
			mUrl	= url ;
			return this ;
		}
		
		public ImageRequest withViewWidth(int width){
			mOption.setViewWidth(width);
			return this ;
		} 
		
		public ImageRequest withViewHeight(int height){
			mOption.setViewHeight(height);
			return this ;
		} 
		
		public ImageRequest withDecodeMaxWidth(int width){
			mOption.setWidth(width);
			return this ;
		} 
		
		public ImageRequest withDecodeMaxHeight(int height){
			mOption.setHeight(height);
			return this ;
		} 
		
		public boolean fetch(){
			
			if(mView == null 
					|| downloader == null
					|| StringUtils.isEmpty(mUrl)) return false ;
			
			downloader.downloadBitmap(mUrl, mView, mOption);
			
			return true ;
		}
	}
}
